#include <iostream>
#include <vector>
#include "MoteCollection.h"
/** 
 * \brief This is game of simon.
 * \file simon.cpp
 * \date 12-4-2007
 * 
 * This is game of Simon to demonstrate use of game API we
 * developed for wireless light motes. 
 */
#define DARK_LEVEL 5
#define LIGHT_LEVEL 10

using namespace std;

enum {RED = 1, YELLOW = 2, BLUE = 3, GREEN = 4, BLACK = 5, WHITE = 6};

/**
 * Plays sounds based on given given id
 * \param id Every possible sounds has been assigned an id 
 */
void PlaySound(string id)
{
	string temp = "mplayer -msglevel all=-1 -really-quiet sounds/" + id + ".wav";
	system(temp.c_str());
}

/**
 * Takes string id as integer and returns color associated with that id.
 * \param id Idenfier for a particular color.
 * \return Name of color as String.
 */
string GetColor(int id)
{
	switch(id)
	{
	case RED:
		return "Red";
	case YELLOW:
		return "Yellow";
	case BLUE:
		return "Blue";
	case GREEN:
		return "Green";
	case BLACK:
		return "Black";
	case WHITE:
		return "White";
	default:
		return "Unknown";
	}
}

int main(int argc, char **argv)
{
	int nColors;
	int level = 0;

	MoteCollection* motes = new MoteCollection();

	MoteBridge bridge(motes);
	
	nColors = 2;
	// Uncomment this section to unable more than 2 colors
	//cout << "Please enter the number of colors to use in this game (2~6): ";
	//cin >> nColors;	

	//registering motes
	for(int i = 1 ; i <= nColors ; i++)
	{
		cout << "Please turn on the mote with color " << GetColor(i) << endl;

		while(true)
		{
			if(motes->Count() >= i)
			{
				cout << "Mote found, registering..." << endl;

				vector<Mote*> moteList = motes->Motes();
	
				moteList[i - 1]->SetTag(i);

				break;
			}
		}
	}

	vector<int> queue;
	vector<Mote*> moteList = motes->Motes();

	cout << "Starting game..." << endl;

	while(true)
	{
		//print the queue contents
		for(int i = 0 ; i < queue.size() ; i++)
		{
			cout << GetColor(queue[i]) << " ";
			PlaySound(GetColor(queue[i]));
			fflush(stdout);
		}

		//add the new color to queue
		queue.push_back((rand()%nColors)+1);
		cout << GetColor(queue.back()) << endl;
		PlaySound(GetColor(queue.back()));
		fflush(stdout);
		level += 1;

		int currentReading = 0;
		bool levelFinished = false;

		system("clear");

		while(true) //look for covered motes
		{
			for(int j = 0 ; j < nColors ; j++)
			{
				if(moteList[j]->Reading() <= DARK_LEVEL) //mote is covered
				{
					cout << GetColor(moteList[j]->GetTag()) << " ";
					PlaySound(GetColor(moteList[j]->GetTag()));
					fflush(stdout);

					if(queue[currentReading] != moteList[j]->GetTag())
					{
						cout << endl << "Game Over...Correct Sequence is:" << endl;
						PlaySound("gameover");

						for(int i = 0 ; i < queue.size() ; i++)
						{
							cout << GetColor(queue[i]) << " ";
							PlaySound(GetColor(queue[i]));
							fflush(stdout);
						}

						cout << endl;

						return 0;
					}
					else if(queue[currentReading] == moteList[j]->GetTag() && currentReading + 1 == level) //got all correct
					{
						levelFinished = true;
						break;
					}
					else
					{
						currentReading++;
						while(moteList[j]->Reading() <= LIGHT_LEVEL); //wait for hand to be removed
					}
				}
			}

			if(levelFinished == true) //got all correct
			{
				break;
			}
		}	
		
		cout << "Got all correct.. moving to level" << level + 1 << endl;		
		PlaySound("correct");
		system("clear");
	}

	return 0;
}
